/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-27
*/
package com.rzico.order.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.report.*;
import com.rzico.order.service.ApiReportService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 报表接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-05-27
 */
@Api(description = "报表接口")
@RestController
@RequestMapping("/admin/apiReport")
public class ApiReportAdminController extends BaseController {

    @Autowired
    private ApiReportService reportService;

    @Autowired
    private EnterpriseService enterpriseService;



    /**
     * 收款统计报表
     *
     * @return
    */
    @ApiOperation("收款统计报表")
    @GetMapping("/selectPaymentRP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
    })
    public CommResult<PaymentRP> selectPaymentRP(Date startDate, Date endDate, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate", DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<PaymentRP> list = reportService.selectPaymentRP(params);
        PageResult<PaymentRP> pageResult = new PageResult<PaymentRP>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }

    /**
     * 收款明细报表
     *
     * @return
     */
    @ApiOperation("收款明细报表")
    @GetMapping("/selectPaymentDetailRP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "paymentPluginId", value = "支付方式", dataType = "String", paramType = "query"),
    })
    public CommResult<PaymentRP> selectPaymentDetailRP(Date startDate, Date endDate,String paymentPluginId, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate", DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (paymentPluginId!=null){
            params.put("paymentPluginId", paymentPluginId);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<PaymentDetailRP> list = reportService.selectPaymentDetailRP(params);
        PageResult<PaymentDetailRP> pageResult = new PageResult<PaymentDetailRP>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }


    /**
     * 订单统计报表
     *
     * @return
     */
    @ApiOperation("订单统计报表")
    @GetMapping("/selectOrderSummaryRP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
    })
    public CommResult<OrderSummaryRP> selectOrderSummaryRP(Date startDate, Date endDate, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate",DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<OrderSummaryRP> list = reportService.selectOrderSummaryRP(params);
        PageResult<OrderSummaryRP> pageResult = new PageResult<OrderSummaryRP>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }


    /**
     * 订单明细报表
     *
     * @return
     */
    @ApiOperation("订单明细报表")
    @GetMapping("/selectOrderDetailRP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "产品ID", dataType = "Long", paramType = "query"),
    })
    public CommResult<OrderDetailRP> selectOrderDetailRP(Date startDate, Date endDate,Long productId, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate",DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (productId!=null){
            params.put("productId",productId);
        }


        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<OrderDetailRP> list = reportService.selectOrderDetailRP(params);
        PageResult<OrderDetailRP> pageResult = new PageResult<OrderDetailRP>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }


    /**
     * 销售统计报表
     *
     * @return
     */
    @ApiOperation("销售统计报表")
    @GetMapping("/selectWarehouseSummaryRP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
    })
    public CommResult<WarehouseSummaryRP> selectWarehouseSummaryRP(Date startDate, Date endDate, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate",DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<WarehouseSummaryRP> list = reportService.selectWarehouseSummaryRP(params);
        PageResult<WarehouseSummaryRP> pageResult = new PageResult<WarehouseSummaryRP>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }


    /**
     * 销售明细报表
     *
     * @return
     */
    @ApiOperation("销售明细报表")
    @GetMapping("/selectWarehouseDetailRP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "产品ID", dataType = "Long", paramType = "query"),
    })
    public CommResult<WarehouseDetailRP> selectWarehouseDetailRP(Date startDate, Date endDate,Long productId, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate",DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (productId!=null){
            params.put("productId",productId);
        }


        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<WarehouseDetailRP> list = reportService.selectWarehouseDetailRP(params);
        PageResult<WarehouseDetailRP> pageResult = new PageResult<WarehouseDetailRP>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }


    /**
     * 配送统计报表
     *
     * @return
     */
    @ApiOperation("配送统计报表")
    @GetMapping("/selectShippingSummaryRP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
    })
    public CommResult<ShippingSummaryRP> selectShippingSummaryRP(Date startDate, Date endDate, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate",DateUtils.truncate(startDate, Calendar.DATE));
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ShippingSummaryRP> list = reportService.selectShippingSummaryRP(params);
        PageResult<ShippingSummaryRP> pageResult = new PageResult<ShippingSummaryRP>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);

    }

}
